<?php

namespace App\Api\Three;

/**
 * https://docs.qq.com/doc/DRUlEamNJWFFhVmVR
 */

use App\Api\ChannelApi;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Illuminate\Support\Facades\DB;
use App\Api\Three\TelecomAse;
use App\Console\Commands\Commission;

use function GuzzleHttp\json_decode;

class DouYinOld
{
    protected $model;
    protected $appKey = '';
    protected $url = '';
    protected $secret = '';
    protected $channel = '';
    protected $config = [];
    protected $HTTPOK = 1; //检查接口返回的数据 返回1 代表返回正确
    protected $root;
    protected $log;
    protected $isTrue = true;
    protected $return_message = '';
    protected $onLineNum = '';


    public function __construct()
    {
        $this->pname = 'DouYinOld';
        $this->log = new Logger('DouYinOld');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/DouYinOld.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
        $this->url = 'http://www.yuxuantongxin.com:32003/api/v1/getDouYinOld';
        // $this->url = 'http://127.0.0.1:8080/api/v1/getDouYinOld';
    }
    // 提交到老系统
    public function orderSubmit($array = [])
    {
        //
        $data = DB::table('douyin_catch')->where($array)->first();
        if (!$data) {
            return [
                'status' => 100,
                'msg' => '提交失败'
            ];
        }
        $res =  $this->sendBefare($this->url, $data, '');
        $this->log->info('参数:' . json_encode($data) . '返回的参数:' . $res);
        if (is_string($res)) {
            $res = json_decode($res, true);
        }
        if (!empty($res['code']) && $res['code'] == 10000) {
            return [
                'status' => 200,
                'msg' => '提交成功'
            ];
        }
        return $res;
    }
    public function sendBefare($url, $data, $headers)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }

        $result = curl_exec($curl); // 执行操作
        curl_close($curl); // 关闭CURL会话
        return $result;
    }
}
